//
//  SEGRedWalletView.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 2017/3/2.
//  Copyright © 2017年 陈区. All rights reserved.
//

#import "SEGRedWalletView.h"
#import "SEGRedWalletPrizeView.h"
@interface SEGRedWalletView ()
@property (nonatomic, assign) SEGRedWalletFromType type;
/**
 回调
 */
@property (nonatomic, copy) SEGAwardRedWalletViewCallBack callback;
@property (nonatomic, strong) SEGRedWalletPrizeView* prizeView;//开红包后中奖界面
@property (nonatomic, strong) UIView* noPrizeView;//开红包后未中奖界面
/*
 抽奖结果data
 */
@property (nonatomic, strong) NSDictionary* resultData;

@property (nonatomic, retain) UIImageView* redWalletView;
@property (nonatomic, retain) UIButton* openButton;

@property (nonatomic, retain) UIView* middleView;
@property (nonatomic, retain) UIImageView* iconView;
@property (nonatomic, retain) UILabel* nameLabel;
@property (nonatomic, retain) UILabel* descLabel;

@property (nonatomic, retain) UILabel* bottomLabel;
@property (nonatomic, retain) UIImageView* leftLine;
@property (nonatomic, retain) UIImageView* rightLine;

@end

@implementation SEGRedWalletView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self customUI];
    }
    return self;
}


- (void)showRedWalletWithType:(SEGRedWalletFromType)type callback:(SEGAwardRedWalletViewCallBack)callback
{
    self.callback = callback;
    self.type = type;
    switch (type) {
        case SEGRedWalletFromType_OpenDoor:
            break;
        case SEGRedWalletFromType_FlashBox:
            break;
        case SEGRedWalletFromType_SignUp:
        {
            [self seg_updateViewWithIcon:[SEGCheckShareRecordManager sharedManager].redwalletModel.userIcon
                                    name:[SEGCheckShareRecordManager sharedManager].redwalletModel.nickname];
            break;
        }
        default:
            break;
    }
}

- (void)showAwardsResultWithDict:(NSDictionary *)resultDit
{
    self.redWalletView.hidden = YES;
    if ([ValueToString([resultDit valueForKey:@"code"]) isEqualToString:@"0"]) {
        self.resultData = [resultDit valueForKey:@"data"];
        self.prizeView.hidden = NO;
        [self.prizeView setViewWithDict:self.resultData];
    }
    else {
        self.noPrizeView.hidden = NO;
    }
}

#pragma mark - 初始化界面

- (void)customUI
{
    self.backgroundColor =kColor_bg3;
    
    //背景视图
    self.redWalletView = [[UIImageView alloc] init];
    self.redWalletView.userInteractionEnabled = YES;
    //    self.bgView.backgroundColor = [UIColor clearColor];
    self.redWalletView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"img_hongbao_bg")];
    [self addSubview:self.redWalletView];
    [self.redWalletView mas_makeConstraints:^(MASConstraintMaker* make) {
        make.centerX.centerY.equalTo(self);
    }];
    
    //开红包按钮
    self.openButton = [[UIButton alloc] init];
    self.openButton.backgroundColor = [UIColor clearColor];
    [self.openButton setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"img_hongbao_open")] forState:UIControlStateNormal];
    [self.openButton addTarget:self action:@selector(seg_openRedWalletAction) forControlEvents:UIControlEventTouchUpInside];
    [self.redWalletView addSubview:self.openButton];
    [self.openButton mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.equalTo(self.redWalletView.mas_top).with.offset(71);
        make.centerX.equalTo(self.redWalletView.mas_centerX);
    }];
    
    self.bottomLabel = [[UILabel alloc] init];
    self.bottomLabel.text = kAppName;
    self.bottomLabel.font = [UIFont systemFontOfSize:13];
    self.bottomLabel.textColor = kColor_S2;
    self.bottomLabel.backgroundColor = [UIColor clearColor];
    [self.redWalletView addSubview:self.bottomLabel];
    [self.bottomLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.centerX.equalTo(self.redWalletView);
        make.bottom.equalTo(self.redWalletView.mas_bottom).with.offset(-20);
    }];
    
    //标题左右横线
    self.leftLine = [[UIImageView alloc] init];
    self.leftLine.image = [UIImage imageWithContentsOfFile:SEGPicName(@"img_hongbao_line_l")];
    [self.redWalletView addSubview:self.leftLine];
    [self.leftLine mas_makeConstraints:^(MASConstraintMaker* make) {
        make.right.equalTo(self.bottomLabel.mas_left).with.offset(-10);
        make.centerY.equalTo(self.bottomLabel.mas_centerY);
    }];
    self.rightLine = [[UIImageView alloc] init];
    self.rightLine.image = [UIImage imageWithContentsOfFile:SEGPicName(@"img_hongbao_line_r")];
    [self.redWalletView addSubview:self.rightLine];
    [self.rightLine mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.equalTo(self.bottomLabel.mas_right).with.offset(10);
        make.centerY.equalTo(self.bottomLabel.mas_centerY);
    }];
}

- (void)seg_updateViewWithIcon:(NSString*)icon name:(NSString*)name
{
    self.middleView = [[UIView alloc] init];
    self.middleView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1];
    self.middleView.layer.masksToBounds = YES;
    self.middleView.layer.cornerRadius = 5;
    [self.redWalletView addSubview:self.middleView];
    [self.middleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.redWalletView).with.offset(15);
        make.right.equalTo(self.redWalletView).with.offset(-15);
        make.top.equalTo(self.redWalletView).with.offset(183);
        make.height.mas_equalTo(61);
    }];
    
    //发红包用户头像
    self.iconView = [[UIImageView alloc] init];
    self.iconView.backgroundColor = [UIColor clearColor];
    //    self.iconView.layer.masksToBounds = YES;
    //    self.iconView.layer.cornerRadius = 5.f;
    [self.middleView addSubview:self.iconView];
    [self.iconView mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.equalTo(self.middleView.mas_left).with.offset(10);
        make.centerY.equalTo(self.middleView.mas_centerY);
        make.width.mas_equalTo(41);
        make.height.mas_equalTo(41);
    }];
    [self.iconView seg_configWithRadius:5];
    //发红包用户名称
    self.nameLabel = [[UILabel alloc] init];
    self.nameLabel.font = [UIFont systemFontOfSize:13];
    self.nameLabel.textColor = kColor_S2;
    self.nameLabel.numberOfLines = 2;
    self.nameLabel.backgroundColor = [UIColor clearColor];
    [self.middleView addSubview:self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.equalTo(self.iconView.mas_right).with.offset(15);
        make.top.equalTo(self.middleView.mas_top).with.offset(12);
    }];
    
    //发红包描述
    self.descLabel = [[UILabel alloc] init];
    self.descLabel.text = @"给你发了个大红包";
    self.descLabel.font = [UIFont systemFontOfSize:13];
    self.descLabel.textColor = kColor_S2;
    self.descLabel.backgroundColor = [UIColor clearColor];
    [self.middleView addSubview:self.descLabel];
    [self.descLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.equalTo(self.iconView.mas_right).with.offset(15);
        make.bottom.equalTo(self.middleView.mas_bottom).with.offset(-12);
    }];
    
    NSString* iconString = [NSString stringWithFormat:@"%@/%@", kClientImageRootURL, icon];
    [self.iconView setImageWithURL:[NSURL URLWithString:iconString] placeholderImage:[UIImage imageWithContentsOfFile:SEGPicName(@"headimg82")]];
    self.nameLabel.text = name;
}

#pragma mark - 按钮响应
- (void)seg_openRedWalletAction
{
    if (self.type == SEGRedWalletFromType_SignUp) {//邀请有礼注册红包
        self.redWalletView.hidden = YES;
        self.prizeView.hidden = NO;
        [self.prizeView setViewWithAward:[SEGCheckShareRecordManager sharedManager].redwalletModel.award prizeType:[SEGCheckShareRecordManager sharedManager].redwalletModel.prizeType];
    }else {
        self.callback(SEGAwardRedWalletButtonType_Open,nil);
    }
}

- (void)seg_closeViewAction
{
    self.callback(SEGAwardRedWalletButtonType_Close,nil);
    [self removeFromSuperview];
}

- (void)seg_seeSoonAction
{
    // ,self.resultData 没有存值的地方
    //self.callback(SEGAwardRedWalletButtonType_SeeSoon,self.resultData);
    self.callback(SEGAwardRedWalletButtonType_SeeSoonSignUp,nil);
    [self seg_closeViewAction];
}

//拆红包后 中奖界面
- (SEGRedWalletPrizeView*)prizeView
{
    if (!_prizeView) {
        _prizeView = [[SEGRedWalletPrizeView alloc] init];
        [self addSubview:_prizeView];
        [_prizeView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        _prizeView.hidden = YES;
        [_prizeView.closeButton addTarget:self action:@selector(seg_closeViewAction) forControlEvents:UIControlEventTouchUpInside];
        [_prizeView.seeSoonButton addTarget:self action:@selector(seg_seeSoonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _prizeView;
}
//拆红包后 未中奖界面
- (UIView *)noPrizeView
{
    if (!_noPrizeView) {
        _noPrizeView = [[UIView alloc] init];
        [self addSubview:_noPrizeView];
        [_noPrizeView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        _noPrizeView.hidden = YES;
        
        //背景视图
        UIImageView *bgView = [[UIImageView alloc] init];
        bgView.userInteractionEnabled = YES;
        //    bgView.backgroundColor = [UIColor clearColor];
        bgView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"img_hongbao_open_02")];
        [_noPrizeView addSubview:bgView];
        [bgView mas_makeConstraints:^(MASConstraintMaker* make) {
            make.centerX.equalTo(self);
            make.centerY.equalTo(self).with.offset(23);
        }];
        
        //关闭按钮
        UIButton *closeButton = [[UIButton alloc] init];
        closeButton.backgroundColor = [UIColor clearColor];
        [closeButton setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"btn_hongbao_close")] forState:UIControlStateNormal];
        [closeButton addTarget:self action:@selector(seg_closeViewAction) forControlEvents:UIControlEventTouchUpInside];
        [_noPrizeView addSubview:closeButton];
        [closeButton mas_makeConstraints:^(MASConstraintMaker* make) {
            make.right.equalTo(bgView);
            make.bottom.equalTo(bgView.mas_top);
        }];
        
        UILabel *descLabel = [[UILabel alloc] init];
        descLabel.text = @"手慢了，红包已派完";
        descLabel.font = [UIFont systemFontOfSize:18];
        descLabel.textColor = [UIColor colorWithRed:247.0/255.0 green:49/255.0 blue:33/255.0 alpha:1];
        [bgView addSubview:descLabel];
        [descLabel mas_makeConstraints:^(MASConstraintMaker* make) {
            make.centerX.equalTo(bgView);
            make.top.equalTo(bgView.mas_top).with.offset(50);
        }];
        
        //我知道了按钮
        UIButton *knowButton = [[UIButton alloc] init];
        knowButton.backgroundColor = [UIColor clearColor];
        [knowButton setTitle:@"我知道了" forState:UIControlStateNormal];
        [knowButton setTitleColor:kColor_W forState:UIControlStateNormal];
        knowButton.titleLabel.font = [UIFont systemFontOfSize:16];
        [knowButton setBackgroundImage:[UIImage imageWithContentsOfFile:SEGPicName(@"btn_hongbao_open_01")] forState:UIControlStateNormal];
        [knowButton addTarget:self action:@selector(seg_closeViewAction) forControlEvents:UIControlEventTouchUpInside];
        [bgView addSubview:knowButton];
        [knowButton mas_makeConstraints:^(MASConstraintMaker* make) {
            make.left.equalTo(bgView).with.offset(15);
            make.right.equalTo(bgView).with.offset(-15);
            make.bottom.equalTo(bgView).with.offset(-15);
        }];
        
    }
    return _noPrizeView;
}

@end
